package online.heycm.rbac.controller.user;

import online.heycm.platform.common.entity.result.Optional;
import online.heycm.platform.common.entity.result.Result;
import online.heycm.rbac.common.config.RequestContext;
import online.heycm.rbac.common.entity.Session;
import online.heycm.rbac.param.user.ConsoleLoginReq;
import online.heycm.rbac.param.user.ConsoleResourceRes;
import online.heycm.rbac.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户服务 - 登入登出
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/12 15:36
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        Optional optional = userService.logout(RequestContext.get());
        return Result.optional(optional);
    }

    /**
     * 管理平台登入
     *
     * @param req
     * @return
     */
    @PostMapping("/console/login")
    public Result<Session> consoleUserLogin(@RequestBody @Validated ConsoleLoginReq req) {
        Optional<Session> optional = userService.consoleUserLogin(req);
        return Result.optional(optional);
    }

    /**
     * 获取用户资源
     *
     * @return
     */
    @GetMapping("/console/resource")
    public Result<List<ConsoleResourceRes>> consoleUserResource() {
        Optional<List<ConsoleResourceRes>> optional = userService.consoleUserResource(RequestContext.get());
        return Result.optional(optional);
    }
}
